package demo6;

//输入18位身份证号码，判断是否成年(满18岁)
//思路一:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Adult01 {
    public static void main(String[] args) {
        System.out.println("430124198101070011:"+hasAdult("430124198101070011"));
        System.out.println("430124202101070011"+hasAdult("430124202101070011"));
    }

    public static boolean hasAdult(String idcard) {
        if(!idcard.matches("^\\d{17}[\\dX]$")) {
            throw new RuntimeException("18位的身份证号码不正确");
        }
        String ymd = idcard.substring(6,14);
        System.out.println(ymd);
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        try {
            Date d = f.parse(ymd);
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            c.add(Calendar.YEAR, 18);
            System.out.println(f.format(c.getTime()));
            return new Date().after(c.getTime());
        } catch (ParseException e) {
            throw new RuntimeException("18位的身份证号码不正确");
        }
    }
}
